<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>2018/12/7</title>
    <script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
    <!--好的代码像粥一样,是得用时间和精力熬的。-->
</head>
<body>
<input type="file">
<button>上传图片</button>
</body>
<script>
    //FormData
/*    window.onload = function () {
        let file = document.getElementsByTagName('input')[0];
        let uploadBtn = document.getElementsByTagName('button')[0];
        uploadBtn.onclick = function () {
            let fd = new FormData();
            axios.get('http://localhost:8888/qiniuToken')
                .then(function (data) {
                    let token = data.data.token;
                    fd.append('token', token);
                    fd.append('file', file.files[0]);
                    return axios.post('http://upload.qiniup.com/', fd, {headers: {'Content-Type': 'multipart/form-data'}});
                })
                .then(function (data) {
                    return axios.post('http://localhost:8888/savePic', {url: `http://pjd004idh.bkt.clouddn.com/${data.data.key}`})
                })
                .then(function (data) {
                    if(data.data.code === 1){
                        console.log('上传成功!');
                    }
                })
                .catch(function (error) {
                    console.log(error);
                })
        }
    }*/
    window.onload = function () {
        let file = document.getElementsByTagName('input')[0];
        let uploadBtn = document.getElementsByTagName('button')[0];
        uploadBtn.onclick = function () {
            let fd = new FormData();
            axios.get('http://localhost:8888/qiniuToken')
                .then(function (data) {
                console.log(data);
                let token = data.data.token;
                fd.append('token', token);
                fd.append('file', file.files[0]);
                return axios.post('http://upload.qiniup.com/', fd, {headers: {'Content-Type': 'multipart/form-data'}});
            })
                .then(function (data) {
                //返回图片的外链
                let url = `http://pjierug5j.bkt.clouddn.com/${data.data.key}`;
                console.log(`http://pjierug5j.bkt.clouddn.com/${data.data.key}`);
                return axios.post('http://localhost:8888/savePic', {url: url})
            })
                .then(function (data) {
                    if (data.data.code === 1) {
                        console.log('上传成功!');
                    }
                })
                .catch(function (error) {
                console.log(error);
            });
        }
    }
</script>
</html>
